{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "apigw-gateway.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "apigw-gateway.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "apigw-gateway.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Common labels
*/}}
{{- define "apigw-gateway.labels" -}}
helm.sh/chart: {{ include "apigw-gateway.chart" . }}
{{ include "apigw-gateway.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end -}}

{{/*
Selector labels
*/}}
{{- define "apigw-gateway.selectorLabels" -}}
app.kubernetes.io/name: {{ include "apigw-gateway.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end -}}

{{/*
Create the name of the service account to use
*/}}
{{- define "apigw-gateway.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
    {{ default (include "apigw-gateway.fullname" .) .Values.serviceAccount.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}}

{{- define "image_pull_secret" }}
{{- printf "{\"auths\": {\"%s\": {\"username\": \"%s\",\"password\": \"%s\", \"auth\": \"%s\"}}}" .Values.image_registry.addr .Values.image_registry.user .Values.image_registry.passwd (printf "%s:%s" .Values.image_registry.user .Values.image_registry.passwd | b64enc) | b64enc }}
{{- end }}


{{/*
Images
*/}}
{{- define "images.redis_image" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.redis_image }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.redis_image }}
{{- end -}}
{{- end }}
{{- define "images.redis_exporter_image" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.redis_exporter_image }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.redis_exporter_image }}
{{- end -}}
{{- end }}
{{- define "images.redis_sentinel_exporter_image" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.redis_sentinel_exporter_image }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.redis_sentinel_exporter_image }}
{{- end -}}
{{- end }}
{{- define "images.apiplane_image" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.apiplane_image }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.apiplane_image }}
{{- end -}}
{{- end }}
{{- define "images.pilot_image" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.pilot_image }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.pilot_image }}
{{- end -}}
{{- end }}
{{- define "images.galley_image" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.galley_image }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.galley_image }}
{{- end -}}
{{- end }}
{{- define "images.rate_limit_image" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.rate_limit_image }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.rate_limit_image }}
{{- end -}}
{{- end }}
{{- define "images.e2e_image" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.e2e_image }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.e2e_image }}
{{- end -}}
{{- end }}
{{- define "images.eureka_demo_image" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.eureka_demo_image }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.eureka_demo_image }}
{{- end -}}
{{- end }}
{{- define "images.consul_demo_image" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.consul_demo_image }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.consul_demo_image }}
{{- end -}}
{{- end }}
{{- define "images.consul_server_image" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.consul_server_image }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.consul_server_image }}
{{- end -}}
{{- end }}
{{- define "images.ingress" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.ingress }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.ingress }}
{{- end -}}
{{- end }}
{{- define "images.http_backend" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.http_backend }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.http_backend }}
{{- end -}}
{{- end }}
{{- define "images.keepalived" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.keepalived }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.keepalived }}
{{- end -}}
{{- end }}
{{- define "images.haproxy_image" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.haproxy_image }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.haproxy_image }}
{{- end -}}
{{- end }}
{{- define "images.http_demo_image" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.http_demo_image }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.http_demo_image }}
{{- end -}}
{{- end }}
{{- define "images.zk_demo_image" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.zk_demo_image }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.zk_demo_image }}
{{- end -}}
{{- end }}
{{- define "images.dubbo_demo_image" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.dubbo_demo_image }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.dubbo_demo_image }}
{{- end -}}
{{- end }}
{{- define "images.soap_demo_image" }}
{{- if eq .Values.cpu_mode "amd64" -}}
{{- print .Values.images_amd64.soap_demo_image }}
{{- else if eq .Values.cpu_mode "arm64" -}}
{{- print .Values.images_arm64.soap_demo_image }}
{{- end -}}
{{- end }}


{{/*根据部署类型定义资源的副本数*/}}
{{- define "replicas.apigw_ingress_keepalived" }}
{{- if eq .Values.replica_semantic "ha" -}}
{{- print .Values.ha_replicas.apigw_ingress_keepalived }}
{{- else if eq .Values.replica_semantic "poc" -}}
{{- print .Values.poc_replicas.apigw_ingress_keepalived }}
{{- end -}}
{{- end }}
{{- define "replicas.apigw_redis" }}
{{- if eq .Values.replica_semantic "ha" -}}
{{- print .Values.ha_replicas.apigw_redis }}
{{- else if eq .Values.replica_semantic "poc" -}}
{{- print .Values.poc_replicas.apigw_redis }}
{{- end -}}
{{- end }}
{{- define "replicas.apigw_redis_haproxy" }}
{{- if eq .Values.replica_semantic "ha" -}}
{{- print .Values.ha_replicas.apigw_redis_haproxy }}
{{- else if eq .Values.replica_semantic "poc" -}}
{{- print .Values.poc_replicas.apigw_redis_haproxy }}
{{- end -}}
{{- end }}
{{- define "replicas.gateway_envoy" }}
{{- if eq .Values.replica_semantic "ha" -}}
{{- print .Values.ha_replicas.gateway_envoy }}
{{- else if eq .Values.replica_semantic "poc" -}}
{{- print .Values.poc_replicas.gateway_envoy }}
{{- end -}}
{{- end }}
{{- define "replicas.apigw_rete_limit" }}
{{- if eq .Values.replica_semantic "ha" -}}
{{- print .Values.ha_replicas.apigw_rete_limit }}
{{- else if eq .Values.replica_semantic "poc" -}}
{{- print .Values.poc_replicas.apigw_rete_limit }}
{{- end -}}
{{- end }}

{{/*定义亲和性*/}}
{{- define "nodeAffinity"}}
nodeAffinity:
  preferredDuringSchedulingIgnoredDuringExecution:
  - weight: 1
    preference:
      nodeSelectorTerms:
      - matchExpressions:
        - key: {{.Values.affinity.node_label_key}}
          operator: In
          values: {{.Values.affinity.node_label_values}}
{{- end}}